package com.foreveross.dao.imp;

import java.util.List;

import org.springframework.stereotype.Component;

import com.foreveross.common.Constants;
import com.foreveross.common.SQLUtil;
import com.foreveross.dao.TenementCostDAO;
import com.foreveross.vo.ProductType;
import com.foreveross.vo.TenementCost;

@Component("tenementCostDAO")
public class TenementCostDAOImp extends SQLUtil implements TenementCostDAO {

	@Override
	public List<TenementCost> findTenementCostList(String weixinID,String userId,
			String houseId, String IpItemName, String status,String resCode,String type, int pageIndex,
			int pageSize) throws Exception {
		StringBuffer queryString = new StringBuffer(
				"select model from TenementCost model where 1=1 ");
		if (!"".equals(weixinID) && null != weixinID) {
			queryString.append(" and model.houseUser in (select b.id from HouseUser b where wechatId= '"+weixinID+"')");
		}
		if (!"".equals(houseId) && null != houseId) {
			queryString.append(" and model.hrHouses = '"+ houseId+"'");
		}
		if (!"".equals(IpItemName) && null != IpItemName) {
			queryString.append(" and model.ipItemName like '%"+ IpItemName+ "%'");
		}
		if (!"".equals(status) && null != status) {
			queryString.append(" and model.status = '"+ status+ "'");
		}
		if (!"".equals(resCode) && null != resCode) {
			queryString.append(" and model.houseUser.resCode like '%"+ resCode+ "%'");
		}
		if(Constants.FIND_COST_CAR.equals(type)){
			queryString.append(" and model.ipItemName like '%车%'");
		}else if(Constants.FIND_COST_TENEMENT.equals(type)){
			queryString.append(" and model.ipItemName not like '%车%'");
		}
		if (!"".equals(userId) && null != userId) {
			this.addAuthorityStr(queryString, userId,  "hrHouses");
		}
		return (List<TenementCost>) findObjectAll(queryString.toString(),
				pageIndex, pageSize);
	}

	@Override
	public long findTenementCostListCount(String weixinID,String userId,
			String houseId, String IpItemName, String status,String resCode,String type) throws Exception {
		StringBuffer queryString = new StringBuffer(
				"select count(*) from TenementCost model where 1=1 ");
		if (!"".equals(weixinID) && null != weixinID) {
			queryString.append(" and model.houseUser in (select b.id from HouseUser b where wechatId= '"+weixinID+"')");
		}
		if (!"".equals(houseId) && null != houseId) {
			queryString.append(" and model.hrHouses = '"+ houseId+"'");
		}
		if (!"".equals(IpItemName) && null != IpItemName) {
			queryString.append(" and model.ipItemName like '%"+ IpItemName+ "%'");
		}
		if (!"".equals(status) && null != status) {
			queryString.append(" and model.status = '"+ status+ "'");
		}
		if (!"".equals(resCode) && null != resCode) {
			queryString.append(" and model.houseUser.resCode like '%"+ resCode+ "%'");
		}
		if(Constants.FIND_COST_CAR.equals(type)){
			queryString.append(" and model.ipItemName like '%车%'");
		}else if(Constants.FIND_COST_TENEMENT.equals(type)){
			queryString.append(" and model.ipItemName not like '%车%'");
		}
		if (!"".equals(userId) && null != userId) {
			this.addAuthorityStr(queryString, userId,  "hrHouses");
		}
		return (Long) findObjectAll(queryString.toString()).get(0);
	}
 

}
